<template>
  <el-row class="container">
    <el-col :span="24">
      <el-row>
        <el-col :span="5">
          <h2>ibeacon设置</h2>
        </el-col>
        <el-col :span="19">
          <el-form :inline="true" :model="form" class="demo-form-inline">
            <el-form-item label="">
              <el-input v-model="form.equipmentNum" placeholder="SN号码" clearable size="mini"></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input v-model="form.tag" placeholder="标签" clearable size="mini"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit" size="mini">搜索</el-button>
            </el-form-item>
            <el-form-item>
              <el-select v-model="form.isValid" placeholder="全部" size="mini" @change="changeState">
                <el-option label="全部" :value="null"></el-option>
                <el-option label="无效" :value="0"></el-option>
                <el-option label="有效" :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-table
        border
        :data="listData.items">
        <el-table-column
          width="200"
          label="操作"
          align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="editItem(scope.row.id)" >编辑
            </el-button>
            <el-button
              type="danger"
              size="mini"
              @click="inValidItem(scope.row.id)" v-if="scope.row.isValid === 1">无效化
            </el-button>
            <el-button
              type="success"
              size="mini"
              @click="validItem(scope.row.id)" v-if="scope.row.isValid === 0">有效化
            </el-button>
            <add-ibeacon-dialog :show.sync="editIbeaconDialogShow" @success="initData" v-if="editId===scope.row.id" :isedit="true" :dataItem="scope.row" :editId.sync="editId"></add-ibeacon-dialog>
          </template>
        </el-table-column>
        <el-table-column
          prop="id"
          label="ID"
          width="150"
          align="center">
        </el-table-column>
        <el-table-column
          prop="tag"
          label="标签"
          align="center">
        </el-table-column>
        <el-table-column
          prop="equipmentNum"
          label="SN号码"
          align="center">
        </el-table-column>
        <el-table-column
          prop="modelName"
          label="型号"
          align="center">
        </el-table-column>
        <el-table-column
          prop="areaName"
          label="绑定区域"
          align="center">
        </el-table-column>
        <el-table-column
          prop="state"
          label="状态"
          width="150"
          align="center">
          <template slot-scope="scope">
            <div>{{scope.row.isValid === 0 ? '无效' : '有效'}}</div>
          </template>
        </el-table-column>
        <el-table-column
          width="180"
          prop="createAt"
          label="创建日期"
          align="center">
          <template slot-scope="scope">
            <div>{{scope.row.createAt | format}}</div>
          </template>
        </el-table-column>
      </el-table>
      <app-pagination
        :currentPage.sync="listData.page"
        :size.sync="listData.size"
        :total="listData.total"
        @handleCurrentChange="initData()"
        @handleSizeChange="initData()"
      ></app-pagination>
      <div class="add_btn_box">
        <el-button @click="addIbeaconDialogShow = true" type="primary" size="mini">新增</el-button>
      </div>
    </el-col>
    <add-ibeacon-dialog :show.sync="addIbeaconDialogShow" @success="initData" v-if="addIbeaconDialogShow"></add-ibeacon-dialog>
  </el-row>
</template>
<script>
  import ApiClient from '@/utils/ApiClient'
  import AppPagination from '@/components/AppPagination'
  import AppSelector from '@/components/AppSelector'
  import AddIbeaconDialog from './AddIbeaconDialog'


  export default {
    components: {
      'app-pagination': AppPagination,
      'app-selector':AppSelector,
      'add-ibeacon-dialog':AddIbeaconDialog,
    },
    data() {
      return {
        listData: {
          page: 1,
          size: 10,
          items: null,
          total: null,
          totalPage: null,
        },
        form: {
          equipmentNum: '',
          tag: '',
          isValid:null,
//          typeId:null,
        },
        addIbeaconDialogShow:false,
        editIbeaconDialogShow:false,
        editId:null,
      }
    },
    methods: {
      initData() {
        ApiClient.get('/backend/ibeacon/list.do', {
          page: this.listData.page,
          size: this.listData.size,
          equipmentNum:this.form.equipmentNum,
          tag: this.form.tag,
          isValid:this.form.isValid,
        }).then(data => {
          this.listData = data
        })
      },
      onSubmit() {
        this.listData.page = 1;
        this.listData.size = 10;
        this.initData()
      },
      validItem(id) {
        ApiClient.put(`/backend/ibeacon/${id}/show.do`).then(data => {
          this.initData()
        }).catch()
      },
      inValidItem(id) {
        ApiClient.put(`/backend/ibeacon/${id}/hide.do`).then(data => {
          this.initData()
        }).catch()
      },
      //状态切换（入库和展出）
      changeState(){
        this.listData.page=1;
        this.listData.size=10;
        this.initData()
      },
//      changeTypeId(){
//        this.listData.page=1;
//        this.listData.size=10;
//        this.initData()
//      },
      //编辑
      editItem(id){
        this.editId = id;
        this.editIbeaconDialogShow = true;
      }
    },
    mounted() {
      this.initData()
    }
  }
</script>
<style scoped lang="scss">
  .container {
    .add_btn_box {
      margin-top: 20px;
      .el-button {
      }
    }
  }
</style>
